#include <iostream>
#include "System.h"
#include "IniFile.h"
#include "Singleton.h"
#include "Server.h"
#include "Timer.h"
#include "Logger.h"
#include "3rdlib/httpcli.h"

using namespace ddserver::server;
using namespace ddserver::utility;
using namespace ddserver::thread;

using namespace std;

void OnTimerHandle1(void* argc)
{
    time_t ticks = time(NULL);
    struct tm* ptm = localtime(&ticks);
    char timestamp[32];
    memset(timestamp, 0, sizeof(timestamp));
    strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", ptm);

    cout<<timestamp<<":OnTimerHandle_1"<<endl;;
}

void OnTimerHandle2(void* argc)
{
    time_t ticks = time(NULL);
    struct tm* ptm = localtime(&ticks);
    char timestamp[32];
    memset(timestamp, 0, sizeof(timestamp));
    strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", ptm);

     cout<<timestamp<<":OnTimerHandle_2"<<endl;
}

void OnTimerHandle3(void* argc)
{
    time_t ticks = time(NULL);
    struct tm* ptm = localtime(&ticks);
    char timestamp[32];
    memset(timestamp, 0, sizeof(timestamp));
    strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", ptm);

    cout<<timestamp<<":OnTimerHandle_3"<<endl;
}

void OnTimerHandle4(void* argc)
{
    time_t ticks = time(NULL);
    struct tm* ptm = localtime(&ticks);
    char timestamp[32];
    memset(timestamp, 0, sizeof(timestamp));
    strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", ptm);
    
    cout<<timestamp<<":OnTimerHandle_4"<<endl;
}

int main()
{
    //测试定时器
    if(0)
    {   
        System * sys = Singleton<System>::instance();
        sys->init();

        Timer* timer = new Timer();
        timer->start(1000*3,3000,OnTimerHandle1,NULL);
        timer->add(1000*2,2000,OnTimerHandle2,NULL);
        timer->add(1000*2,2000,OnTimerHandle3,NULL);
        timer->add(1000*2,2000,OnTimerHandle4,NULL);
        while(1)
        {
            sleep(1);
        }
    }

    //测试http
    if(0)
    {
        std::string http_server_ip = "192.168.80.128";
        int http_server_port = 4444;
        std::string strHttp = "hello world";

        char temp[128];
        sprintf(temp, "http://%s:%d", http_server_ip.c_str(), http_server_port);
        std::string strUrl(temp);
        httplib::Headers m_headers;
        m_headers= {{"content-type","application"}}; 
        httplib::Client cli(strUrl);
        auto res = cli.Post("/test",m_headers,strHttp,"application/json");
        std::cout << "test status:"<<res->status << ",url:"<< strUrl << std::endl;
        std::cout << "test body:"<<res->body << std::endl;
    }

    //网络框架
    if(1)
    {
        System * sys = Singleton<System>::instance();
        sys->init();

        IniFile * ini = Singleton<IniFile>::instance();
        const string & ip = (*ini)["server"]["ip"];
        int port = (*ini)["server"]["port"];
        int threads = (*ini)["server"]["threads"];
        int max_conn = (*ini)["server"]["max_conn"];
        int wait_time = (*ini)["server"]["wait_time"];
        int servertype = (*ini)["server"]["type"];

        Server * server = Singleton<Server>::instance();
        server->set_threads(threads);
        server->set_connects(max_conn);
        server->set_wait_time(wait_time);
        server->set_server_type(servertype);
        server->listen(ip, port);
        server->start();
    }
    return 0;
}
